package cn.tedu.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.Environment;

public class SpringRunner {
    public static void main(String[] args) {
        //1.加载Spring
        System.out.println("1.加载Spring,开始...");
        AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext(SpringConfig.class);
        System.out.println("1.加载Spring,完成...");
        //2.从Spring中获取对象
        // Random random=(Random)ac.getBean("random");
        //不强转
        System.out.println("2.从Spring中获取对象,开始...");
     JdbcConfig jdbcConfig=ac.getBean("jdbcConfig",JdbcConfig.class);
      EnvironmentData environmentData=ac.getBean("environmentData",EnvironmentData.class);
        System.out.println("2.从Spring中获取对象,完成...");
        //3.测试使用对象,以便于观察是否获取到了有效对象
        System.out.println("3.测试使用对象,开始...");
        System.out.println("通过@Values注解获取值");
        System.out.println("\turl>>"+jdbcConfig.getUrl());
        System.out.println("\tdriver>>"+jdbcConfig.getDriver());
        System.out.println("\tusername>>"+jdbcConfig.getUsername());
        System.out.println("\tpassword>>"+jdbcConfig.getPassword());
        System.out.println("\tinit_size>>"+jdbcConfig.getInit_size());
        System.out.println("\tmax_active>>"+jdbcConfig.getMax_active());
        System.out.println("通过自动装配Environment对象获取值");
        Environment env=environmentData.getEnvironment();
        System.out.println("\turl>>"+env.getProperty("spring.jdbc.url"));
        System.out.println("\tdriver>>"+env.getProperty("spring.jdbc.driver"));
        System.out.println("\tusername>>"+env.getProperty("spring.jdbc.username"));
        System.out.println("\tpassword>>"+env.getProperty("spring.jdbc.password"));
        System.out.println("\tinit_size>>"+env.getProperty("spring.jdbc.init-size"));
        System.out.println("\tmax_active>>"+env.getProperty("spring.jdbc.max-active"));
        System.out.println("3.测试使用对象,完成...");
        //4.关闭
        System.out.println("4.关闭,开始...");
        ac.close();
        System.out.println("4.关闭,完成...");
    }
}
